# -*-python2.6.7-webpy0.3-*-
# -*-utf-8-*-
import web

from config.url import urls
from config import settings
from models import model

db = settings.db
admin_render = settings.admin_render
render = settings.render
app = web.application(urls, globals())

class Index:
    
    def GET(self, page=1):
        web.ctx.session.id = 0
        page = int(page)
        perpage = 7
        offset = (page-1)*perpage
        posts = db.select('article', order='id', limit=perpage, offset=offset)
        postcount = db.query('SELECT COUNT(*) AS count FROM article')[0]
        pages = postcount.count / perpage
        if postcount.count % perpage > 0:
            pages += 1
        #if page > pages:
            #raise web.seeother('/error')
        else:
            return render.index(posts=posts, pages=pages)
    
class Login:

    def GET(self):
        """ Show login page """
        web.ctx.session.id = 0
        return admin_render.login()

    def POST(self):
        user_data = dict(username=web.input().get('username', ''), password=web.input().get('password', ''))
        data = db.select('user', user_data, where='username=$username and password=$password')
        if len(data) != 0:
            web.ctx.session.id = 1
            raise web.seeother('/admin')
        else:
            web.ctx.session.id = 0
            raise web.seeother('/error')


class Admin:

    def GET(self):
        """ Show admin page """

        if web.ctx.session.id == 0:
            raise web.seeother('/login')

        else:
            #get table contents
            posts = model.get_posts()
            return admin_render.admin(posts)
        
class View:
    
    def GET(self, id):
        if web.ctx.session.id == 0:
            raise web.seeother('/login')
        else:
            post = model.get_post(int(id))
            return admin_render.view(post)

        
class New:
    
    form = web.form.Form(
        web.form.Textbox('title', web.form.notnull, size=30, description="post title:"),
        web.form.Textarea('content', web.form.notnull, rows=30, cols=80, description="post content:"),
        web.form.Button('Post'),
        )
    
    
    def GET(self):
        if web.ctx.session.id == 0:
            raise web.seeother('/login')
        else:
            form = self.form()
            return admin_render.new(form)
    
    def POST(self):
        form = self.form()
        if not form.validates():
            return admin_render.new(form)
        model.post_new(form.d.title, form.d.content)
        raise web.seeother('/admin')
    

class Edit:
    
    def GET(self, id):
        if web.ctx.session.id == 1:
            post = model.get_post(int(id))
            form = New.form()
            form.fill(post)
            return admin_render.edit(post, form)
        else:
            raise web.seeother('/login')

    def POST(self, id):
        form = New.form()
        #post = model.get_post(int(id))
        if not form.validates():
            return admin_render.edit(post, form)
        else:
            model.post_update(int(id), form.d.title, form.d.content)
            raise web.seeother('/admin')

class Delete:
    
    def GET(self, id):
        if web.ctx.session.id == 0:
            raise web.seeother('/login')
        else:
            model.post_delete(int(id))
            raise web.seeother('/admin')

class Error:
    
    def GET(self):
        web.ctx.session.id = 0
        return admin_render.error()